<script lang="ts" setup>
import { User, Lock, Message, Position } from '@element-plus/icons-vue'
import { alertEffects, ElMessage, type FormInstance, type FormRules } from 'element-plus'
import { reactive } from "@vue/reactivity";
import { ref } from "vue";
import router from '@/router';
import { findpasswordServer } from '@/api/user.js';

const findword = ref();
const message = ref();

interface RuleForm {
    email: String,
    password: String,
    rePassword: String,
    code: String
}

const reData = ref<RuleForm>({
    email: '',
    password: '',
    rePassword: '',
    code: ''
})
const reg_email = /(^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$)/
const reg_password = /(^(?![0-9]+$)(?![a-zA-Z]+$)[0-9a-zA-Z]{5,20}$)/
const isreword = ref(false)

const checkrepassword = (rule, value, callback) => {
    if (value === '') {
        callback(new Error('请再次确认密码'))
    } else if (value !== reData.value.password) {
        callback(new Error('两次密码输入不一致，请再次输入'))
    } else {
        callback()
    }
}

const rules = reactive<FormRules<RuleForm>>({

    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { pattern: reg_password, message: '密码要求5到20位的数字和字母的组合', trigger: 'blur' }
    ],

    email: [
        { required: false, message: '请输入邮箱', trigger: 'blur' },
        { pattern: reg_email, message: '名称允许汉字、字母、数字，域名只允许英文域名', trigger: 'blur' }
    ],

    rePassword: [
        { validator: checkrepassword, trigger: 'blur' }
    ],

    code: [
        { required: false, message: '请输入邮箱', trigger: 'blur' },
        { pattern: reg_email, message: '名称允许汉字、字母、数字，域名只允许英文域名', trigger: 'blur' }
    ],



})


const code = ref()
const cleardata = () => {
    reData.value = {
        password: '',
        rePassword: '',
        email: '',
        code: '',
    }
}

const iscode = () => {
    if (code.value === reData.value.code) {
        isreword.value = true
        ElMessage.success('验证成功')
    } else {
        ElMessage.error('验证失败')
    }
}

import { sendemailServer } from '@/api/user';
const sendemail = async () => {
    if (reData.value.email != '') {
        let result = await sendemailServer(reData.value.email) as any;
        code.value = result.data
        ElMessage.success(result.message ? result.message : '发送成功')
    } else {
        ElMessage.error('邮箱不能为空')
    }
}

const repassword = async () => {

    let result = await findpasswordServer(reData.value) as any;
    ElMessage.success(result.message ? result.message : '修改成功')
    router.push('/login')
}
</script>

<template>
    <el-row class="reword-page">
        <el-col :span="12" class="bg"></el-col>
        <el-col :span="6" :offset="3" class="form">
            <el-form ref="form" size="large" autocomplete="off" v-if="isreword" :model="reData" :rules="rules">
                <el-form-item>
                    <h1>修改密码</h1>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input :prefix-icon="Lock" type="password" placeholder="请输入密码"
                        v-model="reData.password"></el-input>
                </el-form-item>
                <el-form-item prop="rePassword">
                    <el-input :prefix-icon="Lock" type="password" placeholder="请再次输入密码"
                        v-model="reData.rePassword"></el-input>
                </el-form-item>
                <!-- 按钮 -->
                <el-form-item>
                    <el-button class="button" type="primary" auto-insert-space @click="repassword()">
                        确认
                    </el-button>
                </el-form-item>
                <el-form-item class="flex">
                    <el-link type="info" :underline="false" @click="isreword = false; cleardata()">
                        ← 返回
                    </el-link>
                </el-form-item>
            </el-form>

            <el-form ref="form" size="large" autocomplete="off" v-else :model="reData" :rules="rules">
                <el-form-item>
                    <h1>找回密码</h1>
                </el-form-item>
                <el-form-item>
                    <el-input :prefix-icon="Message" placeholder="请输入邮箱" v-model="reData.email"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-input :prefix-icon="Lock" placeholder="请输入邮箱验证码" v-model="reData.code"></el-input>
                    <el-button type="default" size="primary" @click="sendemail()" round style="margin-top: 20px;margin-bottom: 20px;">发送验证码</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button class="button" type="primary" auto-insert-space @click="iscode()">
                        确定
                    </el-button>
                </el-form-item>
                <el-form-item class="flex">
                    <el-link type="info" :underline="false" @click="router.push('/login')">
                        ← 返回
                    </el-link>
                </el-form-item>
            </el-form>


        </el-col>
    </el-row>
</template>

<style scoped>
.demo-image__error .image-slot {
    font-size: 30px;
}

.demo-image__error .image-slot .el-icon {
    font-size: 30px;
}
</style>

<style lang="scss" scoped>
/* 样式 */
.reword-page {
    height: 100vh;
    background-color: #fff;

    .bg {
        background: url('@/assets/logo.png') no-repeat 60% center / 240px auto,
            url('@/assets/login_bg.jpg') no-repeat center / cover;
        border-radius: 0 20px 20px 0;
    }

    .form {
        display: flex;
        flex-direction: column;
        justify-content: center;
        user-select: none;

        .title {
            margin: 0 auto;
        }

        .button {
            width: 100%;
        }

        .flex {
            width: 100%;
            display: flex;
            justify-content: space-between;
        }
    }
}
</style>